<?php

namespace Frieza\Modules\Position\Controllers;

use Doraemon\model\Position\PositionExtend;
use Doraemon\pockets\constInterface\LinkConf;
use Doraemon\service\position\apiModule\Tab4Card;
use Doraemon\tools\BlockType;
use Doraemon\tools\BlockTypeMapping;
use Doraemon\tools\ImgUrl;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;


class Tab4CardController extends BaseController
{
    use BaseTrait;

    public $base_field_ext_type = Tab4Card::EXT_TYPE_FLOW;

    public function extendList(Grid $grid)
    {
        $grid->column('pos', '展示位置')->display(function ($title, Grid\Column $column) {
            $pos = $this->pos % 4;
            $map = ['primary', 'warning', 'success', 'info', 'danger'];
            return $column->label($map[$pos]);
        });
        $grid->column('card_info', '卡片信息')->display(function ($title, Grid\Column $column) {
            $display = [Tab4Card::EXT_MAP[$this->ext_type] ?? Tab4Card::EXT_MAP[Tab4Card::EXT_TYPE_FLOW]];
            if ($this->ext_type == Tab4Card::EXT_TYPE_KA) {
                $display[] = "内容类型:" . BlockType::name($this->obj_type);
                $display[] = "内容id:" . $this->obj_id;
            } else {
                $images[] = "<img src='{$this->pic_url}' style=\"max-width:200px;max-height:200px\" class=\"img img-thumbnail\"/>";
                $display[] = implode('<br/><br/>', $images);
            }

            return implode('<br/>', $display);
        });
    }

    public function saveModule(Request $request, $baseInfoId)
    {
        $pos = $request->input('base.pos', 0);
        if ($pos <= 0) {
            throw new \Exception('展示位置必选大于0');
        }
        $ext_type = $request->input("base.ext_type", "");
        switch ($ext_type) {
            case Tab4Card::EXT_TYPE_FLOW:
                break;
            case Tab4Card::EXT_TYPE_KA:
                $obj = $request->input("base.link", []);
                $link_type = $obj['link_type'] ?? '';
                $link_params = isset($obj['link_params']) ? json_encode($obj['link_params'], JSON_UNESCAPED_UNICODE) : '';
                $objRs = BlockTypeMapping::getObjFromLinkParams($link_type, $link_params);
                $obj_id = $objRs['obj_id'] ?? null;
                $obj_type = $objRs['obj_type'] ?? null;
                PositionExtend::saveTab4Card($baseInfoId, $obj_id, $obj_type);
                break;
        }
    }

    public function beforeTitleForm(Form $form)
    {
        $help = [
            "资源位只在推荐线第一页，可选1-20任意其他数字；",
            "第一位如被轮播资源位占用，则显示在第二位"
        ];
        $form->number('base.pos', '展示位置')->placeholder('位置')->required()->help("<span style='color: red'>" . implode('<br/>', $help) . "</span>");
    }

    public function beforeLinkForm(Form $form)
    {
        $form->radio('base.ext_type', '卡片类型')
            ->options(Tab4Card::EXT_MAP)
            ->when(Tab4Card::EXT_TYPE_FLOW, function (Form $form) {
                $form->imgUpload('base.pic_id', 'Banner卡片', false, ['imgType' => ImgUrl::APPTYPE_OP, 'imgSize' => ImgUrl::IMAGE_SIZE_OP_ORI]);
                $form->multiLinkSelect('base.link', '跳转链接');
            })->when(Tab4Card::EXT_TYPE_KA, function (Form $form) {
                $allowLink = [
                    LinkConf::LINK_PHOTO,
                    LinkConf::LINK_ARTICLE,
                    LinkConf::LINK_GUIDE,
                    LinkConf::LINK_BLANK,
                    LinkConf::LINK_TOPIC_DETAIL,
                    LinkConf::LINK_INSPIRATION_SET,
                    LinkConf::MALL_WIKI_DETAIL,
                    LinkConf::LINK_USER,
                    LinkConf::LINK_APP_WIKI_LIST_DETAIL,
                ];
                $form->multiLinkSelect('base.link', '内容', $allowLink)->help('支持图片note、视频note、整屋案例、指南文章、回答、经验文章、话题、收藏夹、wiki、个人主页、好物清单');
            });
    }

}

